﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

using MAPDavy.Entities;
using MAPDavy.BusinessLogic;

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

using MvcSiteMapProvider;

namespace MAPDavy.WebMVC3.Controllers
{
    public class SeccionController : Controller
    {
        public SeccionController()
        {
            using (IUnityContainer container = new UnityContainer())
            {
                container.LoadConfiguration();
                container.BuildUp(this.GetType(), this);
            }
        }

        [Dependency]
        public ISeccionBL SeccionBL { get; set; }

        [Dependency]
        public IGradeBL GradeBL { get; set; }

        //
        // GET: /Seccion/
        [Authorize(Roles = "admin")]
        public ActionResult Index()
        {
            var seccions = SeccionBL.GetAllFromSeccion();

            return View(seccions);
        }

        [Authorize]
        public ActionResult SeccionServiceJson(int gradeId)
        {
            var cookie = Request.Cookies["workspace"];
            string anio = "0";

            if (cookie != null)
            {
                anio = cookie.Value.ToString();
            }

            var seccions = SeccionBL.GetFromSeccionByAnioAndGradeId(anio,gradeId);


            var lista = from s in seccions
                        select new { Id = s.Id, Seccion = s.ClassName }; 

            return new JsonNetResult(lista);

            //return View();
        }

        [HttpPost]
        [Authorize]
        public ActionResult ExistTutorServiceJson(Int32 anio,Int32 seccionId)
        {
            var existe = SeccionBL.ExisteTutor(anio.ToString(), seccionId);

            return Json(existe, JsonRequestBehavior.AllowGet);

            //return View();
        }

        [Authorize(Roles = "admin")]
        public ActionResult CanDeleteServiceJson(Int32 id)
        {
            var can = SeccionBL.CanRemoveSeccion(id);

            return Json(can, JsonRequestBehavior.AllowGet);
        }

        //
        // GET: /Seccion/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Seccion/Create
        [Authorize(Roles = "admin")]
        public ActionResult Create()
        {
            var grades = GradeBL.GetAllFromGrade();

            ViewData["GradeId"] = new SelectList(grades, "Id", "GradeLevel");

            return View();
        } 

        //
        // POST: /Seccion/Create

        [HttpPost]
        [Authorize(Roles = "admin")]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                Seccion seccion = new Seccion()
                {
                    GradeId = Int32.Parse(collection["GradeId"].ToString()),
                    ClassName = collection["ClassName"].ToString(),
                    Description = collection["Description"].ToString()
                };

                SeccionBL.AddSeccion(seccion);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Seccion/Edit/5
        [MvcSiteMapNode(Title = "Modificar Seccion", ParentKey = "Class", Order = 0)]
        [Authorize(Roles = "admin")]
        public ActionResult Edit(int id=0)
        {
            if (id == 0)
            {
                return RedirectToAction("Index", "Seccion");
            }

            var seccion = SeccionBL.GetFromSeccionById(id);

            if (seccion == null)
            {
                return RedirectToAction("Index", "Seccion");
            }

            var grades = GradeBL.GetAllFromGrade();

            ViewData["GradeId"] = new SelectList(grades, "Id", "GradeLevel",seccion.GradeId);

            return View(seccion);
        }

        //
        // POST: /Seccion/Edit/5

        [HttpPost]
        [Authorize(Roles = "admin")]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                Seccion seccion = new Seccion()
                {
                    Id = id,
                    GradeId = Int32.Parse(collection["GradeId"].ToString()),
                    ClassName = collection["ClassName"].ToString(),
                    Description = collection["Description"].ToString()
                };

                SeccionBL.UpdateSeccion(seccion);
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Seccion/Delete/5
        [HttpPost]
        [Authorize(Roles = "admin")]
        public ActionResult Delete(int id)
        {
            try
            {
                SeccionBL.RemoveSeccion(id);

                return Json(true, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(false, JsonRequestBehavior.AllowGet);
            }
        }
       
    }
}
